import axios from 'axios'


export let adminUser = {
    getUsers: async ()=>{
        return axios.get('http://localhost/api/user/getAll')
        .then(res => res.data)
        .catch(err => err.response.data)
    },
    addUser: async (user) => {
        return axios.post('http://localhost/api/user/add',user)
        .then(res => {
            return res.data
        })
        .catch(err => {
            return err.response.data; 
        })
    },
    checkUserNameExits: async (userName) => {
        return axios.post('http://localhost/api/user/checkUserNameExits',{
            'user_name': userName
        })
        .then(res => {
            return res.data
        })
        .catch(err => {
            return err.response.data; 
        })
    },
    editUserInfo: async (user) => {
        let data = {
            'user_id'   :   user.user_id,
            'user_name' :   user.user_name,
            'name'      :   user.name,
            'is_lock'   :   user.is_lock,
            'is_super'  :   user.is_super,
            'role'      :   user.role,
        }
        return axios.post('http://localhost/api/user/edit',data)
        .then(res => res.data)
        .catch(err => err.response.data)
    },
    delUser: async (user_id) => {
        return axios.post('http://localhost/api/user/del',{user_id: user_id})
                .then(res => {
                    return res.data
                })
                .catch(err => {
                    return err.response.data; 
                })
    }
}

export let adminImg = {
    banner: {
        getAll: async function(){
            return axios.get('http://localhost/api/banner')
            .then(res => res.data)
            .catch(err => err.response.data)
        },
        add: async function(data){
            return axios.post('http://localhost/api/banner/add',data)
            .then(res => res.data)
            .catch(err => err.response.data)
        },
        edit: async function(data){
            return axios.post('http://localhost/api/banner/edit',data)
            .then(res => res.data)
            .catch(err => err.response.data)
        },
        del: async function(id){
            return axios.post('http://localhost/api/banner/del',{
                id: id
            })
            .then(res => res.data)
            .catch(err => err.response.data)
        },
    },
    adv: {
        getAll: async function(){
            return axios.get('http://localhost/api/adv')
            .then(res => res.data)
            .catch(err => err.response.data)
        },
        add: async function(data){
            return axios.post('http://localhost/api/adv/add',data)
            .then(res => res.data)
            .catch(err => err.response.data)
        },
        edit: async function(data){
            return axios.post('http://localhost/api/adv/edit',data)
            .then(res => res.data)
            .catch(err => err.response.data)
        },
        del: async function(id){
            return axios.post('http://localhost/api/adv/del',{
                id: id
            })
            .then(res => res.data)
            .catch(err => err.response.data)
        },
    },
    active: {
        getAll: async function(){
            return axios.get('http://localhost/api/active')
            .then(res => res.data)
            .catch(err => err.response.data)
        },
        add: async function(data){
            return axios.post('http://localhost/api/active/add',data)
            .then(res => res.data)
            .catch(err => err.response.data)
        },
        edit: async function(data){
            return axios.post('http://localhost/api/active/edit',data)
            .then(res => res.data)
            .catch(err => err.response.data)
        },
        del: async function(id){
            return axios.post('http://localhost/api/active/del',{
                id: id
            })
            .then(res => res.data)
            .catch(err => err.response.data)
        },
    },
}

export let adminNews = {
    getAll: async page => {
        return axios.get(`http://localhost/api/news?page=${page}`)
        .then(res => res.data)
        .catch(err => err.response.data)
    },
    add: async data => {
        return axios.post('http://localhost/api/news/add', data)
        .then(res => res.data)
        .catch(err => err.response.data)
    },
    edit: async data => {
        return axios.post('http://localhost/api/news/edit', data)
        .then(res => res.data)
        .catch(err => err.response.data)
    },
    del: async id => {
        return axios.post('http://localhost/api/news/del', {
            id: id
        })
        .then(res => res.data)
        .catch(err => err.response.data)
    },
    getNews: async id => {
        return axios.get(`http://localhost/api/news/getNews?id=${id}`)
        .then(res => res.data)
        .catch(err => err.response.data)
    }
}

export let adminArticle = {
    getAll: async page => {
        return axios.get(`http://localhost/api/article?page=${page}`)
        .then(res => res.data)
        .catch(err => err.response.data)
    },
    add: async data => {
        return axios.post('http://localhost/api/article/add', data)
        .then(res => res.data)
        .catch(err => err.response.data)
    },
    edit: async data => {
        return axios.post('http://localhost/api/article/edit', data)
        .then(res => res.data)
        .catch(err => err.response.data)
    },
    del: async id => {
        return axios.post('http://localhost/api/article/del', {
            id: id
        })
        .then(res => res.data)
        .catch(err => err.response.data)
    },
    getArticle: async id => {
        return axios.get(`http://localhost/api/article/getArticle?id=${id}`)
        .then(res => res.data)
        .catch(err => err.response.data)
    }
}

export let adminSupply = {
    getAll: async page => {
        return axios.get(`http://localhost/api/supply?page=${page}`)
        .then(res => res.data)
        .catch(err => err.response.data)
    },
    add: async data => {
        return axios.post('http://localhost/api/supply/add', data)
        .then(res => res.data)
        .catch(err => err.response.data)
    },
    edit: async data => {
        return axios.post('http://localhost/api/supply/edit', data)
        .then(res => res.data)
        .catch(err => err.response.data)
    },
    del: async id => {
        return axios.post('http://localhost/api/supply/del', {
            id: id
        })
        .then(res => res.data)
        .catch(err => err.response.data)
    },
    getSupply: async id => {
        return axios.get(`http://localhost/api/supply/getSupply?id=${id}`)
        .then(res => res.data)
        .catch(err => err.response.data)
    },
    removeDetailImg: async id => {
        return axios.get(`http://localhost/api/supply/removeDetailImg?id=${id}`)
        .then(res => res.data)
        .catch(err => err.response.data)
    }
}

export let adminDemand = {
    getAll: async page => {
        return axios.get(`http://localhost/api/demand?page=${page}`)
        .then(res => res.data)
        .catch(err => err.response.data)
    },
    add: async data => {
        return axios.post('http://localhost/api/demand/add', data)
        .then(res => res.data)
        .catch(err => err.response.data)
    },
    edit: async data => {
        return axios.post('http://localhost/api/demand/edit', data)
        .then(res => res.data)
        .catch(err => err.response.data)
    },
    del: async id => {
        return axios.post('http://localhost/api/demand/del', {
            id: id
        })
        .then(res => res.data)
        .catch(err => err.response.data)
    },
    getDemand: async id => {
        return axios.get(`http://localhost/api/demand/getDemand?id=${id}`)
        .then(res => res.data)
        .catch(err => err.response.data)
    },
    removeDetailImg: async id => {
        return axios.get(`http://localhost/api/demand/removeDetailImg?id=${id}`)
        .then(res => res.data)
        .catch(err => err.response.data)
    }
}

export let testApi = {
    getLogLength : async ()=> {
        return axios.get('http://localhost/api/usage/getLogLength')
        .then(res => res.data)
        .catch(err => {
            console.error(err); 
        })
    }
}


